﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESRI.ArcGIS.Geodatabase;
using System.Windows.Forms;
using System.Diagnostics;

namespace MyEngineExercise.我的扩展.信息类
{
    /// <summary>
    /// 创建项目红线时，字段的信息，即列信息
    /// </summary>
    public class RedLineFieldInfo
    {
        /// <summary>
        /// 字段名称
        /// </summary>
        private String _fieldName = String.Empty;


        /// <summary>
        /// 字段类型
        /// </summary>
        private esriFieldType _fieldType;


        /// <summary>
        /// 字段长度
        /// </summary>
        private Int32 _fieldLength = 20;


        /// <summary>
        /// 省厅检查要求的长度？
        /// </summary>
        private Int32 _fieldCheckLength = 10;


        /// <summary>
        /// 字段精度
        /// </summary>
        private Int32 _fieldPrecision = 0;


        /// <summary>
        /// 字段默认值
        /// </summary>
        private System.Object _fieldDefaultValue;


        /// <summary>
        /// 字段宽度
        /// </summary>
        private Int32 _fieldScale = 5;


        /// <summary>
        /// 保存每个字段的信息
        /// </summary>
        private static RedLineFieldInfo[] _redLineFieldInfos = null;
        /// <summary>
        /// 保存每个字段的信息
        /// </summary>
        public static RedLineFieldInfo[] RedLineFieldInfos 
        { 
            get
            {
                if (null == _redLineFieldInfos)//如果为空
                    _redLineFieldInfos = Constants.FIELD_NAMES.Select(v => new RedLineFieldInfo(v)).ToArray();

                return _redLineFieldInfos;
            }
        }


        /// <summary>
        /// 获取所有字段
        /// </summary>
        public static IEnumerable<IField> Fields { get { return RedLineFieldInfos.Select(v => v.ConvertToIField()); } }


        /// <summary>
        /// 转换为field类型
        /// </summary>
        /// <returns></returns>
        public IField ConvertToIField()
        {
            IField field = new FieldClass();
            IFieldEdit2 fieldEdit = (IFieldEdit2)field;

            fieldEdit.Name_2 = _fieldName;//shape文件不能超过5个中文字，即10个字符
            fieldEdit.Type_2 = _fieldType;
            fieldEdit.Length_2 = _fieldLength;
            fieldEdit.Precision_2 = _fieldPrecision;
            fieldEdit.DefaultValue_2 = _fieldDefaultValue;
            if (_fieldType == esriFieldType.esriFieldTypeDouble)
            {
                fieldEdit.Scale_2 = _fieldScale;
                /*Debug.Print("_fieldType:" + _fieldType.ToString());
                Debug.Print("_fieldLength:" + _fieldLength.ToString());
                Debug.Print("_fieldPrecision:" + _fieldPrecision.ToString());
                Debug.Print("_fieldDefaultValue:" + _fieldDefaultValue.ToString());
                Debug.Print("_fieldScale 字段宽度:" + _fieldScale.ToString());*/
            }

            return field;
            
        }



        /// <summary>
        /// 静态构造函数,根据字段名称，设置每个字段的对应参数
        /// </summary>
        private RedLineFieldInfo(String fieldName)
        {
            _fieldName = fieldName;

            switch (_fieldName)
            {
                case Constants.NUMBER_OF_POINT_FIELD_NAME:
                    _fieldType = esriFieldType.esriFieldTypeDouble;
                    _fieldLength = 20;
                    _fieldPrecision = 15;
                    _fieldDefaultValue = 0;
                    break;
                case Constants.AREA_FIELD_NAME:
                    _fieldType = esriFieldType.esriFieldTypeDouble;
                    _fieldLength = 20;
                    _fieldPrecision = 15;
                    _fieldDefaultValue = 0;
                    break;
                default:
                    _fieldType = esriFieldType.esriFieldTypeString;
                    _fieldPrecision = 0;
                    _fieldDefaultValue = String.Empty;
                    switch (_fieldName)
                    {
                        case Constants.BLOCK_ID_FIELD_NAME:
                            _fieldLength = _fieldCheckLength = 100;
                            _fieldDefaultValue = "无";
                            break;
                        case Constants.BLOCK_NAME_FIELD_NAME:
                            _fieldLength = 200;
                            _fieldCheckLength = 100;
                            _fieldDefaultValue = "无";
                            break;
                        case Constants.GEOMETRY_TYPE_FIELD_NAME:
                            _fieldLength = _fieldCheckLength = 5;
                            _fieldDefaultValue = "面";
                            break;
                        case Constants.PAGE_NUM_FIELD_NAME:
                            _fieldLength = 40;
                            break;
                        case Constants.USAGE_FIELD_NAME:
                            _fieldLength = _fieldCheckLength = 100;
                            break;
                        case Constants.REMARK_FIELD_NAME:
                            _fieldLength = 200;
                            _fieldCheckLength = 100;
                            break;
                        case Constants.CODE_FIELD_NAME:
                            _fieldLength = _fieldCheckLength = 100;
                            break;
                        default:
                            MessageBox.Show(_fieldName + "  无法建立字段信息", "RedLineFieldsInfo.cs");
                            break;
                    }
                    break;
            }
        }

    }
}
